import { observable, action, autorun, toJS } from 'mobx';
import { WeaTableNew, WeaForm } from 'comsMobx';

const { TableStore } = WeaTableNew;
import { ListStore } from './listStore';
import { BatchShareStore } from './batchShareStore';
import { WeaTools } from 'ecCom';
import { message } from 'antd';
import * as Apis from '../apis/search';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;
export class SearchStore extends ListStore {
	@observable title = '查询客户';
    @observable rightMenu =[];
    @observable baseParams={
      selectType: '',
    }
    @observable showModal = false;
    @observable showModalStatus = false;
    @observable customerIds = '';
    @observable customerStatus = '';
    @observable customerStatusSpan = '';
    @observable assignCrmName = '' ; // 客户名称

    @observable batchShareStore = new BatchShareStore();

    @observable verified = false;
	@observable hasRight = false; // 权限
    @observable shareCondition=[];
    @action
    getTitleInfo=(params = {}) => {
      Apis.getSearchTitleInfo(params).then((data) => {
        if (!data.hasright && typeof (data.hasright) !== 'undefined') {
          this.hasRight = data.hasright;
          this.verified = true;
        } else {
          this.hasRight = true;
          this.verified = true;
          this.title = data.datas.title;
          this.rightMenu = data.datas.rightMenus;
          this.initDatas(params);
          this.doSearch(params);
        }
      });
    }
    @action
    getFormCondition = () => {
      Apis.getFormCondition({ showlevel: 1 }).then((data) => {
        if (data.status == 'success') {
          this.shareCondition = data.data;
        }
      });
    }
    @action
    initDatas=(params = {}) => {
      this.form = new WeaForm();
      this.condition = [];
      Apis.getSearchCondition(params).then((data) => {
        this.condition = data.datas;
        // 根据高级搜索条件初始化form
        this.form && !this.form.isFormInit && this.form.initFormFields(data.datas);
      });
    }

	@action
    doSearch=(params = {}) => {
      this.loading = true;
      // 获取表单的参数值
      const searchParamsAd = this.form.getFormParams();
      const newParams = { ...toJS(this.baseParams), ...searchParamsAd, ...params };
      Apis.getCrmCustomerList(newParams).then((data) => {
        this.tableStore.getDatas(data.sessionkey, params.current || 1);
        this.dataKey = data.sessionkey;
        this.baseParams = { ...this.baseParams, ...params };
        this.loading = false;
      });
    }

    // 修改客户经理，分配客户经理
    @action
    showCustomerModal = (bool, ids, type) => {
      this.showModal = bool;
      this.customerIds = ids;
      if (bool) {
        if (type == 'assign') {
          Apis.getCrmNamesById({ customerIds: ids }).then((data) => {
            this.assignCrmName = data.datas;
          });
        }
      }
    }

    @action
    showCustomerStatusModal = (bool, ids, type) => {
      this.showModalStatus = bool;
      this.customerIds = ids;
      if (!bool) {
        this.customerStatus = '';
      }
    }

    @action
    saveCustomerAssign=(params = {}) => {
      Apis.saveCustomerAssign(params).then((data) => {
        this.customerIds = '';
        window._crmTable.reLoad();
      });
    }

    @action
    saveChangeManager=(params = {}) => {
      Apis.saveChangeManager(params).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel(83551, '保存成功！'));
          this.customerIds = '';
          window._crmTable.reLoad();
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }

    @action
    batChChangeStatus=() => {
      Apis.batChChangeStatus({ customerIds: this.customerIds, customerStatus: this.customerStatus }).then((data) => {
        if (data.status == 'success') {
          message.success(getLabel(83551, '保存成功！'));
          this.customerIds = '';
          this.customerStatus = '';
          window._crmTable.reLoad();
          this.showCustomerStatusModal(false);
        } else {
          message.error(getLabel(83437, '出现错误！') + data.msgcode);
        }
      });
    }
    // 批量共享
    @action
    saveCustomerShare =(params = {}) => {
      if (params.info != '[]') {
        this.batchShareStore.handleShareDialog(false);
        params = { ...params, selectType: this.baseParams.selectType };
        // if(sharetype=="6"){
        //     let jobtitlelevel=parseInt(params.jobtitlelevel);
        //     params.jobtitlelevel=(jobtitlelevel-1);
        // }
        Apis.saveCustomerShare(params).then((data) => {
          if (data.status == 'success') {
            message.success(getLabel(83551, '保存成功！'));
            window._crmTable.reLoad();
          } else {
            message.error(getLabel(83437, '出现错误！') + data.msgcode);
          }
        });
      } else {
        message.error(`${getLabel(384175, '请先添加权限再保存')}!`);
      }
    }

    @action
	onShowColumn=() => { // 显示定制列
	  this.tableStore.setColSetVisible(true);
	  this.tableStore.tableColSet(true);
	}

	@action
	clearStatus=() => {
	  this.baseParams = {
	    selectType: '',
	  };
	  this.showModal = false;
	  this.showSearchAd = false;
	  // this.showCustomerModal = false;
	  this.clearFormFields();
	}

    @action
    setCustomerStatus = (v, name) => {
      this.customerStatus = v;
      this.customerStatusSpan = name;
    }
}

